package com.gimi.cloud.jimi.manage.consumer.service.app.impl;

import com.gimi.cloud.jimi.common.enums.ResponseCodeEnum;
import com.gimi.cloud.jimi.common.exception.MsgException;
import com.gimi.cloud.jimi.common.utils.JsonUtil;
import com.gimi.cloud.jimi.common.utils.StringUtil;
import com.gimi.cloud.jimi.common.utils.Validates;
import com.gimi.cloud.jimi.common.utils.reqhead.Response;
import com.gimi.cloud.jimi.common.utils.reqhead.Util;
import com.gimi.cloud.jimi.dto.app.ColumnProdDTO;
import com.gimi.cloud.jimi.manage.consumer.feign.AdAndColumnFeignService;
import com.gimi.cloud.jimi.manage.consumer.service.app.ColumnProdService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;
import org.springframework.web.multipart.support.StandardMultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @auther loongs
 * @date 2018/1/16  15:53
 */
@Service
public class ColumnProdServiceImpl implements ColumnProdService {
    private static final Logger logger = LoggerFactory.getLogger(ColumnProdServiceImpl.class);
    private AdAndColumnFeignService adAndColumnFeignService;

    @Autowired
    public ColumnProdServiceImpl(AdAndColumnFeignService adAndColumnFeignService) {
        this.adAndColumnFeignService = adAndColumnFeignService;
    }

    @Override
    public Response getColumnProdList(String requestBody, HttpServletRequest request, HttpServletResponse response) throws MsgException {
        logger.info("请求参数===>" + JsonUtil.replaceNow(requestBody));
        ColumnProdDTO columnProdDTO = JsonUtil.jsonToBean(requestBody, ColumnProdDTO.class);
        if (StringUtil.isBlank(columnProdDTO.getPageSize()) || StringUtil.isBlank(columnProdDTO.getCurrentPage()) || StringUtil.isBlank(columnProdDTO.getColumnId())) {
            throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
        }
        return adAndColumnFeignService.getColumnProdList(columnProdDTO);
    }

    @Override
    public Response addColumnProd(String requestBody, HttpServletRequest request, HttpServletResponse response) throws MsgException {
        logger.info("请求参数===>" + JsonUtil.replaceNow(requestBody));
        ColumnProdDTO columnProdDTO = JsonUtil.jsonToBean(requestBody, ColumnProdDTO.class);
        if (StringUtil.isBlank(columnProdDTO.getSort()) || StringUtil.isBlank(columnProdDTO.getColumnId())
                || StringUtil.isBlank(columnProdDTO.getProdId())) {
            throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
        }
        return adAndColumnFeignService.addColumnProd(columnProdDTO);
    }

    @Override
    public Response addBatchColumnProd(String requestBody, HttpServletRequest request, HttpServletResponse response) throws MsgException {
        logger.info("请求参数===>" + JsonUtil.replaceNow(requestBody));
        ColumnProdDTO columnProdDTO = JsonUtil.jsonToBean(requestBody, ColumnProdDTO.class);
        if (StringUtil.isBlank(columnProdDTO.getColumnId()) || columnProdDTO.getAddBatchParam() == null ||
                (columnProdDTO.getAddBatchParam() != null && columnProdDTO.getAddBatchParam().size() == 0)) {
            throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
        }
        //判断加入进来的商品ID时候有重复，有重复的话，不能进行批量添加
        List<String> listTemp = new ArrayList<>();
        List<Map<String, Object>> list = columnProdDTO.getAddBatchParam();
        for (Map<String, Object> map : list) {
            if (StringUtil.isBlank(map.get("prodId")) || StringUtil.isBlank(map.get("sort"))) {
                throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR,"批量添加失败，商品和排序不能为空！");
            }
            listTemp.add(map.get("prodId").toString());
        }
        if (!Validates.hasSame(listTemp)) {
            throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR, "商品重复，一个栏目不能同时拥有两个或者两个以上的商品，请确认后重试！");
        }
        return adAndColumnFeignService.addBatchColumnProd(columnProdDTO);
    }


    @Override
    public Response goToaddColumnProd(String requestBody, HttpServletRequest request, HttpServletResponse response) throws MsgException {
        logger.info("请求参数===>" + JsonUtil.replaceNow(requestBody));
        ColumnProdDTO columnProdDTO = JsonUtil.jsonToBean(requestBody, ColumnProdDTO.class);
        if (StringUtil.isBlank(columnProdDTO.getColumnId())) {
            throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
        }
        return adAndColumnFeignService.goToaddColumnProd(columnProdDTO);
    }

    @Override
    public Response goToEditColumnProd(String requestBody, HttpServletRequest request, HttpServletResponse response) throws MsgException {
        logger.info("请求参数===>" + JsonUtil.replaceNow(requestBody));
        ColumnProdDTO columnProdDTO = JsonUtil.jsonToBean(requestBody, ColumnProdDTO.class);
        if (StringUtil.isBlank(columnProdDTO.getId())) {
            throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
        }
        return adAndColumnFeignService.goToEditColumnProd(columnProdDTO);
    }

    @Override
    public Response editColumnProd(String requestBody, HttpServletRequest request, HttpServletResponse response) throws MsgException {
        logger.info("请求参数===>" + JsonUtil.replaceNow(requestBody));
        ColumnProdDTO columnProdDTO = JsonUtil.jsonToBean(requestBody, ColumnProdDTO.class);
        if (StringUtil.isBlank(columnProdDTO.getSort()) || StringUtil.isBlank(columnProdDTO.getId())) {
            throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
        }
        return adAndColumnFeignService.editColumnProd(columnProdDTO);
    }

    @Override
    public Response delColumnProd(String requestBody, HttpServletRequest request, HttpServletResponse response) throws MsgException {
        logger.info("请求参数===>" + JsonUtil.replaceNow(requestBody));
        ColumnProdDTO columnProdDTO = JsonUtil.jsonToBean(requestBody, ColumnProdDTO.class);
        if (StringUtil.isBlank(columnProdDTO.getId())) {
            throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
        }
        return adAndColumnFeignService.delColumnProd(columnProdDTO);
    }

    @Override
    public Response upLoadColumnProdFile(HttpServletResponse response, HttpServletRequest request) throws Exception {
        StandardMultipartHttpServletRequest multipartHttpServletRequest = (StandardMultipartHttpServletRequest) request;
        MultiValueMap multipartFiles = multipartHttpServletRequest.getMultiFileMap();
        return adAndColumnFeignService.upLoadColumnProdFile(multipartFiles);
    }
}
